package com.yilin.tms.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.commons.util.DateTimeUtil;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.data.contracted.ContractBody;
import com.yilin.tms.core.entity.data.receipt.ReceiptOrder;
import com.yilin.tms.core.entity.order.transit.TransitOrder;
import com.yilin.tms.core.entity.order.transit.TransitOrderNote;
import com.yilin.tms.core.remote.IOrderRemote;
import com.yilin.tms.data.mapper.ReceiptOrderMapper;
import com.yilin.tms.data.service.IContractService;
import com.yilin.tms.data.service.IReceiptOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 电子签收单Service
 *
 * @author zhangjinyuan
 * @date 2022/11/1
 */
@Service
@Transactional
public class ReceiptOrderService extends ServiceImpl<ReceiptOrderMapper, ReceiptOrder> implements IReceiptOrderService {

    @Resource
    private ReceiptOrderMapper receiptOrderMapper;
    @Resource
    private IContractService contractService;
    @Resource
    private IOrderRemote orderRemote;

    @Override
    public ReceiptOrder getSaveElcReceiptOrderByNoteId(String transitOrderNoteId) {
        ReceiptOrder elcReceiptOrder = getElcReceiptOrderByNoteId(transitOrderNoteId);
        if (elcReceiptOrder!=null) return elcReceiptOrder;
        TransitOrderNote transitOrderNote = ParamUtil.isSuccess(orderRemote.getTransitOrderNoteById(transitOrderNoteId));
        TransitOrder transitOrder = ParamUtil.isSuccess(orderRemote.getTransitOrderById(transitOrderNote.getTransitOrderId()));
        return saveElcReceiptOrder(transitOrder, transitOrderNote);
    }

    @Override
    public ReceiptOrder getElcReceiptOrderByNoteId(String noteId) {
        QueryWrapper<ReceiptOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("transit_order_note_id",noteId);
        return receiptOrderMapper.selectOne(queryWrapper);
    }

    @Override
    public ReceiptOrder saveElcReceiptOrder(TransitOrder transitOrder, TransitOrderNote transitOrderNote) {
        ContractBody contractBody = contractService.saveOrGetTransitPromise(transitOrder.getId());
        ReceiptOrder receiptOrder = new ReceiptOrder();
//        receiptOrder.setPlatId(transitOrder.getPlatId());
//        receiptOrder.setReceiptNo(DateTimeUtil.dateNo(""));
//        receiptOrder.setPlatName(transitOrder.getPlatName());
//        receiptOrder.setZonePlatId(transitOrder.getZonePlatId());
//        receiptOrder.setZonePlatName(transitOrder.getZonePlatName());
        receiptOrder.setContractNo(contractBody.getPromiseNo());
        receiptOrder.setTransitOrderId(transitOrder.getId());
        receiptOrder.setTransitOrderNoteId(transitOrderNote.getId());
        receiptOrder.setTransportNo(transitOrder.getTransportNo());
        receiptOrder.setShipperOrganizeName(transitOrder.getShipperOrganizeName());
        receiptOrder.setRobOrganizeName(transitOrder.getRobOrganizeName());
        receiptOrder.setTransitTruckNo(transitOrder.getTransitTruckNo());
        receiptOrder.setTransitTruckUserName(transitOrder.getTransitTruckUserName());
        receiptOrder.setDeliveryName(transitOrderNote.getConsignorName());
        receiptOrder.setDeliveryPhone(transitOrderNote.getConsignorPhone());
        receiptOrder.setDeliveryTime(transitOrderNote.getNoteDeliverTime());
        receiptOrder.setDeliveryAddress(transitOrderNote.getConsignorAddress());
        receiptOrder.setReceiptName(transitOrderNote.getConsigneeName());
        receiptOrder.setReceiptPhone(transitOrderNote.getConsigneePhone());
        receiptOrder.setReceiptTime(transitOrderNote.getNoteReceiptTime());
        receiptOrder.setReceiptAddress(transitOrderNote.getConsigneeAddress());
        receiptOrder.setReceiptImg(transitOrderNote.getNoteReceiptImg());
        receiptOrder.setReceiptSignImg(transitOrderNote.getNoteReceiptSignImg());
        receiptOrderMapper.insert(receiptOrder);
        return receiptOrder;
    }



}
